package com.tencent.liteav.demo.superplayer.ui.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.tencent.liteav.demo.superplayer.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerDef;
import com.tencent.liteav.demo.superplayer.SuperPlayerGlobalConfig;

/**
 * Floating window mode playback control
 * <p>
 * Slide to move the floating window, click the floating window to return
 * to window mode {@link #onTouchEvent(MotionEvent)}
 * <p>
 * Close the floating window {@link #onClick(View)}
 *
 *
 * 悬浮窗模式播放控件
 * <p>
 * 1、滑动以移动悬浮窗，点击悬浮窗回到窗口模式{@link #onTouchEvent(MotionEvent)}
 * <p>
 * 2、关闭悬浮窗{@link #onClick(View)}
 */
public class FloatPlayer extends BasicPlayer{
    private RelativeLayout dynamicWatermarkLayout;
    private RelativeLayout mCloudVideoViewLayout;
    private float mXDownInScreen;   // Distance from the press event to the left edge of the screen.
    private float mYDownInScreen;   // Distance from the press event to the top edge of the screen.
    private float mXInScreen;       // Distance from the sliding event to the left edge of the screen.
    private float mYInScreen;       // Distance from the sliding event to the top edge of the screen.
    private float mXInView;         // Distance from the sliding event to the left edge of itself.
    private float mYInView;         // Distance from the sliding event to the top edge of itself.

    public FloatPlayer(Context context) {
        super(context);
    }

    public FloatPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public int getLayoutId() {
        return R.layout.superplayer_vod_player_float;
    }

    @Override
    public void findViews() {
        super.findViews();
        dynamicWatermarkLayout = findViewById(R.id.superplayer_dynamic_watermark_layout);
        mCloudVideoViewLayout = findViewById(R.id.superplayer_cloud_video_view_layout);
    }

    public void addDynamicWatermarkView(View view) {
        if (dynamicWatermarkLayout != null) {
            dynamicWatermarkLayout.addView(view);
        }
    }

    public void addCloudVideoView(View view) {
        if (mCloudVideoViewLayout != null) {
            mCloudVideoViewLayout.addView(view);
        }
    }

    /**
     * Override touch event listener to move the floating window with finger.
     *
     * 重写触摸事件监听，实现悬浮窗随手指移动
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                removeHideViewTask();
                mXInView = event.getX();
                mYInView = event.getY();
                mXDownInScreen = event.getRawX();
                mYDownInScreen = event.getRawY() - getStatusBarHeight();
                mXInScreen = event.getRawX();
                mYInScreen = event.getRawY() - getStatusBarHeight();

                break;
            case MotionEvent.ACTION_MOVE:
                mXInScreen = event.getRawX();
                mYInScreen = event.getRawY() - getStatusBarHeight();
                updateViewPosition();

                break;
            case MotionEvent.ACTION_UP:
                // If the finger does not slide, it is regarded as a click and returns to window mode.
                if (mXDownInScreen == mXInScreen && mYDownInScreen == mYInScreen) {
                    show();
                    hideViewDelay();
                }
                break;
            default:
                break;
        }

        return true;
    }

    /**
     * Get the height of the system status bar.
     *
     * 获取系统状态栏高度
     */


    /**
     * Update the position information of the floating window, and implement the floating window movement in
     * the callback {@link Callback#onFloatPositionChange(int, int)}.
     *
     * 更新悬浮窗的位置信息，在回调{@link Callback#onFloatPositionChange(int, int)}中实现悬浮窗移动
     */
    private void updateViewPosition() {
        int x = (int) (mXInScreen - mXInView);
        int y = (int) (mYInScreen - mYInView);
        SuperPlayerGlobalConfig.TXRect rect = SuperPlayerGlobalConfig.getInstance().floatViewRect;
        if (rect != null) {
            rect.x = x;
            rect.y = y;
        }
        if (mControllerCallback != null) {
            mControllerCallback.onFloatPositionChange(x, y);
        }
    }
}
